package zju.ccnt.rest.repository;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;
import zju.ccnt.rest.domain.BusinessResource;

/**
 * Created by zha on 14/11/11.
 */
@Repository
public interface BusinessResourceRepository extends PagingAndSortingRepository<BusinessResource, String>{
    Page<BusinessResource> findByBusinessIdAndClassId(String businessId, String classId, Pageable pageable);
    BusinessResource findByHashValue(String hashValue);

    /**
     * 获取指定商家的指定物力资源
     * @param businessId
     *          商家ID
     * @param hashValue
     *          物力资源hashvalue
     * @return
     * NOTE: 应该只有一个
     */
    BusinessResource findByBusinessIdAndHashValue(String businessId, String hashValue);

    @Modifying
    @Transactional
    @Query(value = "update business_resource set has_uploaded = 1 where hash_value=:hashValue", nativeQuery = true)
    int uploadedBusineddResource(@Param("hashValue") String hashValue);
}
